package lzyc.pointsManagement.business.notice.service.impl;

import lzyc.pointsManagement.business.config.BizType;
import lzyc.pointsManagement.business.notice.domain.NoticeInfo;
import lzyc.pointsManagement.business.notice.mapper.NoticeInfoMapper;
import lzyc.pointsManagement.business.notice.service.NoticeInfoService;
import lzyc.pointsManagement.business.points.domain.PointsInfo;
import lzyc.pointsManagement.business.points.service.PointsInfoService;
import lzyc.pointsManagement.business.service.AttachmentsService;
import lzyc.pointsManagement.business.userdept.domain.SysUserDept;
import lzyc.pointsManagement.business.userdept.service.SysUserDeptService;
import lzyc.pointsManagement.common.core.text.Convert;
import lzyc.pointsManagement.common.utils.DateUtils;
import lzyc.pointsManagement.common.utils.ShiroUtils;
import lzyc.pointsManagement.common.utils.StringUtils;
import lzyc.pointsManagement.common.utils.uuid.IdUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * (NoticeInfo)表服务实现类
 *
 * @author yangyang
 * @since 2024-03-10 17:02:25
 */
@Service("noticeInfoService")
public class NoticeInfoServiceImpl implements NoticeInfoService {
    @Resource
    private NoticeInfoMapper noticeInfoMapper;

    @Resource
    private AttachmentsService attachmentsService;

    @Resource
    private SysUserDeptService sysUserDeptService;

    @Resource
    private PointsInfoService pointsInfoService;

    /**
     * 通过ID查询单条数据
     *
     * @param noticeid 主键
     * @return 实例对象
     */
    @Override
    public NoticeInfo queryById(String noticeid) {
        NoticeInfo noticeInfo = this.noticeInfoMapper.queryById(noticeid);
        String loginName = ShiroUtils.getLoginName();
        if (!isReadNotice(noticeInfo, loginName)) {
            readNotice(noticeInfo, loginName);
        }
        if (StringUtils.isNotEmpty(noticeInfo.getImage())){
            noticeInfo.setAttachmentsDetail(Arrays.asList(attachmentsService.getAttachmentsDtoById(noticeInfo.getImage())));
        }
        return noticeInfo;
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<NoticeInfo> queryAllByLimit(int offset, int limit) {
        return this.noticeInfoMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 根据条件分页查询公告数据
     *
     * @param noticeInfo
     * @return 对象列表
     */
    @Override
    public List<NoticeInfo> queryAll(NoticeInfo noticeInfo) {
        return this.noticeInfoMapper.queryAll(noticeInfo);
    }

    /**
     * 新增数据
     *
     * @param noticeInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(NoticeInfo noticeInfo) {
        if (StringUtils.isEmpty(noticeInfo.getNoticeid())) {
            noticeInfo.setNoticeid(IdUtils.randomUUID());
        }
        if (StringUtils.isNull(noticeInfo.getCreatetime())) {
            noticeInfo.setCreatetime(DateUtils.getNowDate());
        }
        return this.noticeInfoMapper.insert(noticeInfo);
    }

    /**
     * 修改数据
     *
     * @param noticeInfo 实例对象
     * @return 实例对象
     */
    @Override
    public int update(NoticeInfo noticeInfo) {
        noticeInfo.setCreatetime(DateUtils.getNowDate());
        return this.noticeInfoMapper.update(noticeInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param ids 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(String ids) {
        String[] noticeIds = Convert.toStrArray(ids);
        deletePointInfo(noticeIds);
        return this.noticeInfoMapper.deleteById(noticeIds);
    }

    /**
     * 当最新动态未读时，获取最新动态列表
     *
     * @return
     */
    @Override
    public Optional<Object> getUnread() {
        List<NoticeInfo> list = queryAllByLimit(0, 1);
        if (list.isEmpty()) {
            return Optional.empty();
        }
        NoticeInfo noticeInfo = list.get(0);
        String loginName = ShiroUtils.getLoginName();
        if (isReadNotice(noticeInfo, loginName)) {
            return Optional.empty();
        }
        readNotice(noticeInfo, loginName);
        if (StringUtils.isNotEmpty(noticeInfo.getImage())){
            noticeInfo.setAttachmentsDetail(Arrays.asList(attachmentsService.getAttachmentsDtoById(noticeInfo.getImage())));
        }
        return Optional.ofNullable(noticeInfo);
    }

    /**
     * 获取未读用户列表
     *
     * @param noticeID
     * @return
     */
    @Override
    public List<SysUserDept> userUnread(String noticeID) {
        return sysUserDeptService.userUnread(noticeID);
    }

    /**
     * 获取已读用户列表
     *
     * @param noticeID
     * @return
     */
    @Override
    public List<SysUserDept> userRead(String noticeID) {
        return sysUserDeptService.userRead(noticeID);
    }

    /**
     * 阅读公告
     *
     * @param noticeInfo
     * @param loginName
     */
    private void readNotice(NoticeInfo noticeInfo, String loginName) {
        pointsInfoService.insert(getNoticePointsInfo(noticeInfo, loginName));
    }

    private PointsInfo getNoticePointsInfo(NoticeInfo noticeInfo, String loginName) {
        PointsInfo pointsInfo = new PointsInfo();
        pointsInfo.setProjectId(noticeInfo.getNoticeid());
        pointsInfo.setType(BizType.Notice.getCode());
        pointsInfo.setScore(0);
        pointsInfo.setPointsUserId(loginName);
        pointsInfo.setPointsDate(DateUtils.getNowDate());
        return pointsInfo;
    }

    /**
     * 判断公告已读
     * 
     * @param noticeInfo
     * @param loginName
     * @return
     */
    private boolean isReadNotice(NoticeInfo noticeInfo, String loginName) {
        if (pointsInfoService.checkPointExist(noticeInfo.getNoticeid(), loginName)) {
            return true;
        }
        return false;
    }

    /**
     * 删除积分信息
     *
     * @param ids
     */
    private void deletePointInfo(String[] ids) {
        pointsInfoService.deleteByProjectId(ids);
    }
}